﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;

namespace WCFDuplex.Services
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class MessageService : IMessageService,IDisposable
    {
        public void RegisterClient(object client)
        {
            IMessageServiceCallback callback = OperationContext.Current.GetCallbackChannel<IMessageServiceCallback>();

            OperationContext.Current.Channel.Closing+=new EventHandler(Channel_Closing);

            ChannelManager.GetInstance().AddChannel(callback);
        }

        protected void Channel_Closing(object sender, EventArgs e)
        {
            ChannelManager.GetInstance().RemoveChannel((IMessageServiceCallback)sender);
        }

        public void Dispose()
        {
            ChannelManager.GetInstance().ClearChannels();
        }
    }
}
